﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
    public partial class ConnDuctFrom : System.Windows.Forms.Form
	{
		public ConnSelectStyle DuctConn { get; set; }

		public bool Flag
		{
			get
			{
				return this.flag;
			}
			set
			{
				this.flag = value;
			}
		}

		public ConnDuctFrom(Document document)
		{
			this.InitializeComponent();
            List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{DC50C835-AC79-4B07-80BD-A75379471CB3}", null);
            //base.Icon = Resource.YArchitechIcon;
			this.doc = document;
		}

		public ConnDuctFrom()
		{
			this.InitializeComponent();
            List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{DC50C835-AC79-4B07-80BD-A75379471CB3}", null);
		}

		private void ConnDuctFrom_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
                base.Location = new System.Drawing.Point(230, 170);
			}
			this.ductShape = Convert.ToInt32(this.userSetting.GetDuctShapeStyle());
			if (this.userSetting.GetRevitPeizhi() == "0")
			{
				this.cbdefault.Checked = false;
			}
			else
			{
				this.cbdefault.Checked = true;
			}
			this.InitialButton();
			this.flag = this.cbdefault.Checked;
		}

		private void InitialButton()
		{
			string ductConnStyle = this.userSetting.GetDuctConnStyle();
			uint num = 0;//<PrivateImplementationDetails>.ComputeStringHash(ductConnStyle);
			if (num <= 3120479248u)
			{
				if (num != 445416469u)
				{
					if (num != 3059995963u)
					{
						if (num != 3120479248u)
						{
							return;
						}
						if (!(ductConnStyle == "Elbow"))
						{
							return;
						}
						this.SelectButton(this.btnElbow);
						return;
					}
					else
					{
						if (!(ductConnStyle == "Cross"))
						{
							return;
						}
						this.SelectButton(this.btnCross);
						return;
					}
				}
				else
				{
					if (!(ductConnStyle == "Tee"))
					{
						return;
					}
					this.SelectButton(this.btnTee);
					return;
				}
			}
			else if (num <= 3264564162u)
			{
				if (num != 3177711879u)
				{
					if (num != 3264564162u)
					{
						return;
					}
					if (!(ductConnStyle == "Back"))
					{
						return;
					}
					this.SelectButton(this.btnBack);
					return;
				}
				else
				{
					if (!(ductConnStyle == "Reduce"))
					{
						return;
					}
					this.SelectButton(this.btnReduce);
					return;
				}
			}
			else if (num != 3872925180u)
			{
				if (num != 4106703835u)
				{
					return;
				}
				if (!(ductConnStyle == "Round"))
				{
					return;
				}
				this.SelectButton(this.btnRound);
				return;
			}
			else
			{
				if (!(ductConnStyle == "Lateral"))
				{
					return;
				}
				this.SelectButton(this.btnTakeOff);
				return;
			}
		}

		private void SelectButton(Button button)
		{
			this.isSingleClick = false;
			button.PerformClick();
			button.Select();
			button.Focus();
			this.isSingleClick = true;
		}

		private void btnConn_Click(object sender, EventArgs e)
		{
            System.Drawing.Color backColor = this.gbStyle.BackColor;
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			ConnectDuctManage connectDuctManage = new ConnectDuctManage();
			this.userSetting = new HVACUserSetting();
			this.ductShape = Convert.ToInt32(this.userSetting.GetDuctShapeStyle());
			string name = button.Name;
            uint num = 0;//<PrivateImplementationDetails>.ComputeStringHash(name);
			if (num <= 554991360u)
			{
				if (num != 54761778u)
				{
					if (num != 480137033u)
					{
						if (num == 554991360u)
						{
							if (name == "btnBack")
							{
								this.DuctConn = ConnSelectStyle.Back;
								this.connStyle = connectDuctManage.GetDuctConnStyle(this.userSetting.GetDuctConnBackStyle());
							}
						}
					}
					else if (name == "btnReduce")
					{
						this.DuctConn = ConnSelectStyle.Reduce;
						this.connStyle = connectDuctManage.GetDuctConnStyle(this.userSetting.GetDuctConnReduceStyle());
					}
				}
				else if (name == "btnElbow")
				{
					this.DuctConn = ConnSelectStyle.Elbow;
					if (this.ductShape == 0)
					{
						this.connStyle = connectDuctManage.GetDuctConnStyle(this.userSetting.GetDuctConnRoundElbowStyle());
					}
					else
					{
						this.connStyle = connectDuctManage.GetDuctConnStyle(this.userSetting.GetDuctConnElbowStyle());
					}
				}
			}
			else if (num <= 1489196377u)
			{
				if (num != 1180119459u)
				{
					if (num == 1489196377u)
					{
						if (name == "btnTakeOff")
						{
							this.DuctConn = ConnSelectStyle.Lateral;
							this.connStyle = connectDuctManage.GetDuctConnStyle(this.userSetting.GetDuctConnLateralStyle());
						}
					}
				}
				else if (name == "btnTee")
				{
					this.DuctConn = ConnSelectStyle.Tee;
					if (this.ductShape == 0)
					{
						this.connStyle = connectDuctManage.GetDuctConnStyle(this.userSetting.GetDuctConnRoundTeeStyle());
					}
					else
					{
						this.connStyle = connectDuctManage.GetDuctConnStyle(this.userSetting.GetDuctConnTeeStyle());
					}
				}
			}
			else if (num != 3660307893u)
			{
				if (num == 3873876965u)
				{
					if (name == "btnRound")
					{
						this.DuctConn = ConnSelectStyle.Round;
						this.connStyle = connectDuctManage.GetDuctConnStyle(this.userSetting.GetDuctConnRoundStyle());
					}
				}
			}
			else if (name == "btnCross")
			{
				this.DuctConn = ConnSelectStyle.Cross;
				if (this.ductShape == 0)
				{
					this.connStyle = connectDuctManage.GetDuctConnStyle(this.userSetting.GetDuctConnRoundCrossStyle());
				}
				else
				{
					this.connStyle = connectDuctManage.GetDuctConnStyle(this.userSetting.GetDuctConnCrossStyle());
				}
			}
			foreach (object obj in this.gbStyle.Controls)
			{
				Button button2 = obj as Button;
				if (button2 != null)
				{
					button2.BackColor = backColor;
					if (button2 == button)
					{
                        button2.BackColor = System.Drawing.Color.LightSkyBlue;
					}
				}
			}
			string imageName = connectDuctManage.GetImageName(this.connStyle);
			if (File.Exists(imageName))
			{
				this.pbPreview.Image = Image.FromFile(imageName);
			}
			if (this.isSingleClick)
			{
				int bVk = 27;
				int dwFlags = 0;
				int dwFlags2 = 2;
				int wMsg = 7;
                Cursor.Position = new System.Drawing.Point(Cursor.Position.X + 20, Cursor.Position.Y);
				this.isPicking = true;
				ConnDuctFrom.SendMessage(HYDuctFittingConnect.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				ConnDuctFrom.keybd_event((byte)bVk, 0, dwFlags, 0);
				ConnDuctFrom.keybd_event((byte)bVk, 0, dwFlags2, 0);
				this.isPicking = true;
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void ConnDuctFrom_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			ConnDuctFrom.SendMessage(HYDuctFittingConnect.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			ConnDuctFrom.keybd_event((byte)bVk, 0, dwFlags, 0);
			ConnDuctFrom.keybd_event((byte)bVk, 0, dwFlags2, 0);
			this.isPicking = false;
		}

		private void ConnDuctFrom_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
			this.userSetting.WriteDuctConnStyle(this.DuctConn.ToString());
		}

		private void ConnDuctFrom_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void ConnDuctFrom_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				ConnDuctFrom.SendMessage(HYDuctFittingConnect.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void ConnDuctFrom_Leave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				ConnDuctFrom.SendMessage(HYDuctFittingConnect.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void btnElbow_MouseDown(object sender, MouseEventArgs e)
		{
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			if (this.userSetting != null)
			{
				this.userSetting = new HVACUserSetting();
			}
			if (e.Button == MouseButtons.Right)
			{
				this.SelectButton(button);
				string name = button.Name;
				uint num = 0;//<PrivateImplementationDetails>.ComputeStringHash(name);
				if (num <= 554991360u)
				{
					if (num != 54761778u)
					{
						if (num != 480137033u)
						{
							if (num == 554991360u)
							{
								if (name == "btnBack")
								{
									this.DuctConn = ConnSelectStyle.Back;
									SelectBackFittingForm selectBackFittingForm = new SelectBackFittingForm(this.userSetting.GetDuctConnBackStyle());
									selectBackFittingForm.ShowDialog();
									this.connStyle = selectBackFittingForm.ductConn;
								}
							}
						}
						else if (name == "btnReduce")
						{
							this.DuctConn = ConnSelectStyle.Reduce;
							SelectReduceFittingForm selectReduceFittingForm = new SelectReduceFittingForm(this.userSetting.GetDuctConnReduceStyle());
							selectReduceFittingForm.ShowDialog();
							this.connStyle = selectReduceFittingForm.ductConn;
						}
					}
					else if (name == "btnElbow")
					{
						this.DuctConn = ConnSelectStyle.Elbow;
						SelectElbowFittingForm selectElbowFittingForm = new SelectElbowFittingForm();
						selectElbowFittingForm.ShowDialog();
						this.connStyle = selectElbowFittingForm.ductConn;
					}
				}
				else if (num <= 1489196377u)
				{
					if (num != 1180119459u)
					{
						if (num == 1489196377u)
						{
							if (name == "btnTakeOff")
							{
								this.DuctConn = ConnSelectStyle.Lateral;
								SelectSideFittingForm selectSideFittingForm = new SelectSideFittingForm(this.userSetting.GetDuctConnLateralStyle());
								selectSideFittingForm.ShowDialog();
								this.connStyle = selectSideFittingForm.ductConn;
							}
						}
					}
					else if (name == "btnTee")
					{
						this.DuctConn = ConnSelectStyle.Tee;
						SelectTeeFittingForm selectTeeFittingForm = new SelectTeeFittingForm();
						selectTeeFittingForm.ShowDialog();
						this.connStyle = selectTeeFittingForm.ductConn;
					}
				}
				else if (num != 3660307893u)
				{
					if (num == 3873876965u)
					{
						if (name == "btnRound")
						{
							this.DuctConn = ConnSelectStyle.Round;
							SelectRoundFittingForm selectRoundFittingForm = new SelectRoundFittingForm(this.userSetting.GetDuctConnRoundStyle());
							selectRoundFittingForm.ShowDialog();
							this.connStyle = selectRoundFittingForm.ductConn;
						}
					}
				}
				else if (name == "btnCross")
				{
					this.DuctConn = ConnSelectStyle.Cross;
					SelectCrossFittingForm selectCrossFittingForm = new SelectCrossFittingForm();
					selectCrossFittingForm.ShowDialog();
					this.connStyle = selectCrossFittingForm.ductConn;
				}
				ConnectDuctManage connectDuctManage = new ConnectDuctManage();
				this.userSetting = new HVACUserSetting();
				string imageName = connectDuctManage.GetImageName(this.connStyle);
				if (File.Exists(imageName))
				{
					this.pbPreview.Image = Image.FromFile(imageName);
				}
			}
			if (e.Button == MouseButtons.Right || e.Clicks == 2)
			{
				int bVk = 27;
				int dwFlags = 0;
				int dwFlags2 = 2;
				int wMsg = 7;
                Cursor.Position = new System.Drawing.Point(Cursor.Position.X + 20, Cursor.Position.Y);
				this.isPicking = true;
				ConnDuctFrom.SendMessage(HYDuctFittingConnect.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				ConnDuctFrom.keybd_event((byte)bVk, 0, dwFlags, 0);
				ConnDuctFrom.keybd_event((byte)bVk, 0, dwFlags2, 0);
				this.isPicking = true;
			}
		}

		private void cbdefault_CheckedChanged(object sender, EventArgs e)
		{
			if (this.cbdefault.Checked)
			{
				this.flag = true;
				this.btnBack.Enabled = false;
				this.userSetting.WriteRevitPeizhi("1");
                if (this.btnBack.BackColor == System.Drawing.Color.LightSkyBlue)
				{
					this.btnConn_Click(this.btnElbow, null);
					return;
				}
			}
			else
			{
				this.flag = false;
				this.btnBack.Enabled = true;
				this.userSetting.WriteRevitPeizhi("0");
			}
		}

		private HVACUserSetting userSetting = new HVACUserSetting();

		private Document doc;

		public ConnectDuctStyle connStyle = ConnectDuctStyle.Empty;

		private int ductShape = -1;

		private bool flag;

		public bool isPicking = true;

		public bool isSingleClick = true;

		private ControlInfoManager m_thisManger;
	}
}
